﻿package pl.krasi.facebook 
{
	import com.facebook.data.events.FacebookEventData;
	import com.facebook.data.friends.GetFriendsData;
	import com.facebook.net.FacebookCall;
	import com.facebook.session.WebSession;
	import com.facebook.utils.FacebookConnectUtil;
	import com.facebook.utils.FacebookSessionUtil;
	import com.facebook.events.FacebookEvent;
	import com.facebook.commands.friends.*;
	import com.facebook.commands.users.*;
	import com.facebook.*;
	import com.facebook.data.users.*;
	import com.facebook.Facebook;
	
	import com.facebook.utils.FacebookSessionUtil;
	import com.facebook.commands.notifications.*;
	import com.facebook.commands.stream.*;
	import flash.text.TextField;
	
	import flash.display.LoaderInfo;
	import flash.events.EventDispatcher;
	import com.facebook.*;
	import flash.net.URLLoader;
	
	import nl.demonsters.debugger.MonsterDebugger;
	
	/**
	 * ...
	 * @author Mblaszczyk
	 */
	public class Model extends EventDispatcher
	{
		private var _apiKey:String;
		private var _secret:String;
		public var _status:TextField;
		private var debugger:MonsterDebugger;
		
		protected var fbSessionUtil:FacebookSessionUtil;
		protected var facebook:Facebook;
		protected var loader:URLLoader; 
		
		public function Model(loaderInfo:LoaderInfo, status:TextField) 
		{
			debugger = new MonsterDebugger(this);
			
			//setup API key & secret
			_status = status;
			_apiKey = "bbf6fcbcc422e375d75b973c5d5674fb";
			_secret = "e5f45000d71bfe8ba588193c4c2436a1"

			facebook = new Facebook();
			
			fbSessionUtil = new FacebookSessionUtil(_apiKey, _secret, loaderInfo);
			fbSessionUtil.login();
			facebook = fbSessionUtil.facebook;
			
			fbSessionUtil.addEventListener(FacebookEvent.CONNECT, onConnect, false, 0, true);
			fbSessionUtil.addEventListener(FacebookEvent.LOGOUT, onLogout, false, 0, true);
			fbSessionUtil.addEventListener(FacebookEvent.LOGIN_FAILURE, onLoginFailur, false, 0, true);
			fbSessionUtil.addEventListener(FacebookEvent.ERROR, onError, false, 0, true);
			
			MonsterDebugger.trace(this, "Hello World!");
			
			/*

			facebook = fbSessionUtil.facebook;
			fbSessionUtil.login();
			*/
			
			
			/*
			 fbSessionUtil.addEventListener(FacebookEvent.PERMISSIONS_LOADED, onPermissionsLoaded, false, 0, true);
			fbSessionUtil.addEventListener(FacebookEvent.PERMISSION_STATUS, onPermissionStatus, false, 0, true);
			fbSessionUtil.addEventListener(FacebookEvent.PERMISSION_CHANGE, onPermissionChange, false, 0, true);
			 * */
		}
		
		
		private function getSomeData() : void {
			
			var call:FacebookCall = new GetFriends();
			facebook.post(call);
			
			call.addEventListener(FacebookEvent.COMPLETE, onGetFriendsUID, false, 0, true);
			
		}
		
		private function onGetFriendsUID(e:FacebookEvent) : void {
			//var user=(e.data as GetInfoData).userCollection.getItemAt(0) as FacebookUser;
			//var title : String = "Hello " + user.first_name + " " + user.last_name;
			//trace (title);
			
			_status.text += e.data.toString();
			_status.text +="=============";
			_status.text +=e.data.rawResult;
			
			var fData:GetFriendsData = e.data as GetFriendsData;
			var friends_uid : Array = new Array();
			//var friends : Array = (e.data as GetFriendsData).friends.toArray();
			
			//for each(var friend : FacebookUser in friends) {
			//	friends_uid.push(friend.uid);
			//}
          
			//trace (fData.friends);
			
			//trace ("Lacznie znajomych: ", fData.friends.length)
			//for (var i:int = 0; i < fData.friends.length; i++ ) {
				//  var friendID:Number = fData.friends.getItemAt(i).uid;
				 // _status.text =friendID);
			//}
		}
		
		/**
		 * Event handler functions
		 * @param	e
		 */
		private function onConnect(e:FacebookEvent) : void {
			trace ("polaczono")
			_status.text += "connected\n";
			
			getSomeData();
		}
		private function onLogout (e:FacebookEvent) : void {
			_status.text += "logged out\n";
		}
		
		private function onLoginFailur(e:FacebookEvent) : void {
			_status.text +="loggin error\n";
		}
		
		private function onError(e:FacebookEvent) : void {
			_status.text +="Error\n";
		}
		
	}
	
}